package ywpt.service.config;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import ywpt.core.dao.HibernateEntityDao;
import ywpt.model.BankInfo;
import ywpt.web.common.PublicFunction;

public class BankInfoServiceImpl extends HibernateEntityDao<BankInfo> implements BankInfoService{
	public BankInfoServiceImpl(){
		
	}

	@Override
	public String createBankInfoCode() {
		DetachedCriteria query = DetachedCriteria.forClass(BankInfo.class, "bankInfo");
		String typeCode="";
		String code="";
		Integer maxId=new Integer(0);
		query.addOrder(Order.desc("id"));
		List<BankInfo> bankInfos = this.getBankInfos(query);
		if ( bankInfos.iterator().hasNext() )	
		{
			code = bankInfos.iterator().next().getCode();		
			maxId = new Integer(code);
		}
		typeCode=PublicFunction.getNextCode("", maxId, false,3); 
		return typeCode;
	}

	@Override
	public BankInfo getBankInfoById(Long id) {
		return this.get(id);
	}

	@Override
	public int getBankInfoCountByCriteria(DetachedCriteria query) {
		return this.getCountByCriteria(query);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<BankInfo> getBankInfos(DetachedCriteria query,
			int pageSize, int startIndex) {
		return this.findAllByCriteria(query, pageSize, startIndex);
	}

	@Override
	public List<BankInfo> getBankInfos() {
		return this.getAll();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<BankInfo> getBankInfos(DetachedCriteria query) {
		return this.findAllByCriteria(query);
	}

	@Override
	public void removeBankInfo(Long id) {
		super.removeById(id);
	}

	@Override
	public void saveBankInfo(BankInfo bankInfo) {
		super.save(bankInfo);
	}

	@Override
	public void updatBankInfo(BankInfo bankInfo) {
		super.update(bankInfo);
	}

	
	@SuppressWarnings("unchecked")
	@Override
	public List<BankInfo> getBankInfosByTypeName(String type, int start,
			int size) {
		DetachedCriteria query = DetachedCriteria.forClass(BankInfo.class, "bankInfo");
		if ( type != null && !type.trim().equals("") )
		{
			query.add(Restrictions.or(Restrictions.like("type", "%" + type + "%"),  
							          Restrictions.like("code", "%" + type + "%")));
		}
		query.addOrder(Order.asc("code"));
		List<BankInfo> bankInfoList = new ArrayList<BankInfo>();
		List<BankInfo> bankInfoLists = new ArrayList<BankInfo>();
		BankInfo bankInfo =new BankInfo();
		
		bankInfoList.addAll(this.findAllByCriteria(query, size, start));
		int i = 1;
		for ( Iterator<BankInfo> itr = bankInfoList.iterator(); itr.hasNext(); i++ ) 
		{
			bankInfo = itr.next();
			String ss = PublicFunction.format(bankInfo.getCode() + "",5, "  ", 1)
			          + bankInfo.getType(); 
			
			bankInfo.setType(ss);
			bankInfoLists.add(bankInfo);
		}
		return bankInfoLists;
	}

	@Override
	public int getBankInfosCountByName(String type) {
		DetachedCriteria query = DetachedCriteria.forClass(BankInfo.class, "bankInfo");
		if ( type != null && !type.trim().equals("") )
		{
			query.add(Restrictions.or(Restrictions.like("type", "%" + type + "%" ),  
							          Restrictions.like("code", "%" + type + "%" )));
		}
		query.setProjection(null);
		return this.getCountByCriteria(query);
	}
}
